﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreNhaDat;
using CoreProvince;

namespace BatDongSan.Modules
{
    public partial class UcSearchBrokersBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string _keyWord;
            int _loaiNhaDatId;
            int _nhaDatId;
            int _provinceId;
            int _wardId;
            try
            {
                try
                {
                    _keyWord = Request.QueryString["key"];
                }
                catch
                {
                    _keyWord = "";
                }
                _loaiNhaDatId = Convert.ToInt32(Request.QueryString["loai"]);
                _nhaDatId = Convert.ToInt32(Request.QueryString["nhadat"]);
                _provinceId = Convert.ToInt32(Request.QueryString["tp"]);
                _wardId = Convert.ToInt32(Request.QueryString["quan"]);
                
                if (!IsPostBack)
                {
                    BindingDdlProvince(_provinceId);
                    BindingDdlWard(_provinceId, _wardId);
                    BindingNhaDat(_nhaDatId);
                    BindingLoaiNhaDat(_loaiNhaDatId, _nhaDatId);
                    txtTuKhoa.Text = _keyWord;
                }
            }
            catch { }
        }

        protected void BindingDdlProvince(int idProvince)
        {
            try
            {
                ddlProvince.Items.Clear();
                List<nlit_Province> list = ProvinceBll.GetList(true, Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        nlit_Province module = list[i];
                        ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                        ddlProvince.Items.Add(li);
                        if (list[i].Id == idProvince)
                        {
                            ddlProvince.SelectedIndex = i;
                        }
                    }
                }
                ListItem li1 = new ListItem("Chọn Tỉnh / Thành phố", "0");
                ddlProvince.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingDdlWard(int idProvince, int idWard)
        {
            try
            {
                ddlWard.Items.Clear();
                if (idProvince > 0)
                {
                    List<nlit_Ward> list = WardBll.GetList(false, true, idProvince, Utility.strConnect);
                    if (list.Count > 0)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            nlit_Ward module = list[i];
                            ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                            ddlWard.Items.Add(li);
                            if (list[i].Id == idWard)
                            {
                                ddlWard.SelectedIndex = i;
                            }
                        }

                    }
                }
                ListItem li1 = new ListItem("Chọn Quận / Huyện", "0");
                ddlWard.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingNhaDat(int id)
        {
            try
            {
                ddlNhaDat.Items.Clear();
                List<nlit_NhaDat> list = NhaDatBll.GetList(false, true, Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        nlit_NhaDat module = list[i];
                        ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                        ddlNhaDat.Items.Add(li);
                        if (list[i].Id == id)
                        {
                            ddlNhaDat.SelectedIndex = i;
                        }
                    }
                }
                ListItem li1 = new ListItem("Chọn loại tin", "0");
                ddlNhaDat.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingLoaiNhaDat(int id, int nhaDatId)
        {

            try
            {
                ddlLoaiNhaDat.Items.Clear();
                List<nlit_LoaiNhaDat> list = LoaiNhaDatBll.GetList(false, true, nhaDatId, Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        nlit_LoaiNhaDat module = list[i];
                        ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                        ddlLoaiNhaDat.Items.Add(li);
                        if (list[i].Id == id)
                        {
                            ddlLoaiNhaDat.SelectedIndex = i;
                        }
                    }
                }
                ListItem li1 = new ListItem("Chọn loại nhà đất", "0");
                ddlLoaiNhaDat.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void ddlNhaDat_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingLoaiNhaDat(0, Convert.ToInt32(ddlNhaDat.SelectedValue));
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingDdlWard(Convert.ToInt32(ddlProvince.SelectedValue), 0);
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            string re = "";

            re += "?nhadat=" + ddlNhaDat.SelectedValue;
            re += "&loai=" + ddlLoaiNhaDat.SelectedValue;
            re += "&tp=" + ddlProvince.SelectedValue;
            re += "&quan=" + ddlWard.SelectedValue;
            if (!string.IsNullOrEmpty(txtTuKhoa.Text.Trim()))
            {
                re += "&key=" + txtTuKhoa.Text.Trim();
            }
            string link = "/SearchBrokers.aspx" + re;
            Response.Redirect(link);
        }
    }
}